package com.aixin.project.db;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.aixin.project.data.CommonConstants;
import com.aixin.project.data.Expert;
import com.aixin.project.util.AxMemcached;

/**
 * Created by IntelliJ IDEA. User: Sufeng Date: 12-4-14 Time: 下午6:16 To change
 * this template use File | Settings | File Templates.
 */
public class ExpertDao extends Dao{
	
	public static List<Expert> allexpertList(){
		return list("select * from expert", Expert.class);
	}

    public static List<Integer> allexpertIdList(){
        List<Integer> list = null;
        List<Map<String, Object>> mapList = list("select id from expert");
        if(mapList!=null && mapList.size()>0){
            list = new ArrayList<Integer>();
            for(Map map:mapList){
                list.add((Integer) map.get("id"));
            }
        }
        return list;
   	}

	public static Expert find(long uid) {
		return (Expert) find("select * from expert where id=?", Expert.class,
				uid);
	}

	public static Expert find(String username, String password) {
		return (Expert) find(
				"select * from expert where username=? and password=?",
				Expert.class, username, password);
	}

	public static List<Expert> expertList(int num, int page) {
		return list("select * from expert order by id asc limit ?,?", Expert.class,
				(page - 1) * num,num);
	}

	public static Object totalRs() {
		return findBy("select count(*) from expert", 1, null);
	}

	public static List<Expert> expertList() {
		return list(
				"select id,username,gender,education,verified,name from expert",
				Expert.class);
	}

	public static int updateStatus(long userid, int status) {
		return update("update expert set verified=? where id=?", status, userid);
	}

	public static int updatepwd(long userid, String pwd) {
		return update("update expert set password=? where id=?", pwd, userid);
	}

	public static long addExpert(Expert expert) {
		return insert(
				"insert into expert(username,password,description,gender,education,post,name) values (?,?,?,?,?,?,?)",
				expert.getUsername(), expert.getPassword(), expert
						.getDescription(), expert.getGender(), expert
						.getEducation(), expert.getPost(), expert.getName());
	}
	
	public static void deleteExpert(String id){
		int result = update("delete from expert where id=?",id);
		if(result>0)
			AxMemcached.delete(CommonConstants.MKEY_EXPERT + id);
	}
	
	 public static long count(int status){
	        String sql = "select count(*) c from expert";
	        if(status!=-1)
	            sql += " where verified="+status;
	        return  (Long)findBy(sql, "c", null);
	    }
	 
	 public static List<Expert> listPagenation(int page,int psize,int status){
	        int start = (page-1)*psize;
	        StringBuffer sql = new StringBuffer("select * from expert");
	        if(status!=-1)
	            sql.append(" where verified=").append(status);
	        sql.append(" order by id desc limit ?,?");
	        return list(sql.toString(),Expert.class,start,psize);
	    }
}
